package com.entity;

import com.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.*;


/**
 * @author Zxeao~Lenovo
 * @description 注册函数，传入要注册的账号和密码
 * @return 注册成功返回真，否则为假（账号已存在）
 * @throw 若碰到错误，会向外抛出
 */
public class SignUp {

    private final String account;
    private final String password;

    public SignUp(String account, String password) throws Exception{
        this.account = account;
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        this.password = new BigInteger(1, md.digest()).toString(16);
    }


    public boolean signUp() throws Exception{

            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            //连接数据库
            Connection conn = DriverManager.getConnection(Config.JDBCURL, "zxeao", "123456");

            //通过连接对象 获得Statement 对象
            String sql = String.format("INSERT INTO `account` (account,password) VALUES ('%s','%s')", account,password);
            PreparedStatement  statement  = conn.prepareStatement(sql);

            //执行SQL
            int res = statement.executeUpdate();

            statement.close();
            conn.close();

        return res == 1;
    }
}